package com.yjhb.android.sbz.v2.fragment;

import android.view.View;

import androidx.fragment.app.Fragment;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.LoginActivity;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.mvp.game.GamePresenter;
import com.yjhb.android.sbz.tool.XWTool;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.view.EmptyView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 创建者 zx
 * 创建时间 2021/1/15 9:04
 */
public class V2GameFragment extends BaseFragment {
    private final String TAG = "GameFragment";

    Fragment fragment;

    @BindView(R.id.emptyView)
    EmptyView emptyView;


    @Override
    protected GamePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_v2_game;
    }

    @Override
    protected void initUI() {
        XWTool.init(getContext());
        emptyView.getEmptyReloadTv().setText("去登录");
        refresh();
    }

    @Override
    protected void initEvent() {
        EventBus.getDefault().register(this);
        emptyView.setOnReloadClickListener(new EmptyView.OnReloadClickListener() {
            @Override
            public void onReloadClick(EmptyView emptyView) {
                ZxActivityTool.skipActivity(getContext(), LoginActivity.class);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void refresh() {
        fragment = XWTool.getInstance().getFragment();
        if (fragment == null) {
            emptyView.setVisibility(View.VISIBLE);
            emptyView.showFail("您还有登录哦~", true);
        } else {
            emptyView.setVisibility(View.GONE);
            getChildFragmentManager().beginTransaction()
                    .replace(R.id.layout_content, fragment)
                    .commitAllowingStateLoss();
        }

    }
}
